﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.XtraScheduler;

namespace Agnes.TaskManager
{
    /// <summary>
    /// TaskEditorView.xaml 
    /// </summary>
    public partial class TaskEditorView : UserControl
    {
        private RecurrenceInfo _recurrenceInfo = null;

        public RecurrenceInfo RecurrenceInfo
        {
            get { return _recurrenceInfo; }
            set
            {
                _recurrenceInfo = value;



                switch (_recurrenceInfo.Type)
                {
                    case RecurrenceType.Daily:
                        rbDaily.IsChecked = true;
                        if (_recurrenceInfo.WeekDays == WeekDays.WorkDays)
                        {
                            rbWorkDay.IsChecked = true;
                        }
                        else
                        {
                            rbEveryDay.IsChecked = true;
                        }
                        break;
                    case RecurrenceType.Weekly:
                        rbWeekly.IsChecked = true;
                        break;
                    case RecurrenceType.Monthly:
                        rbMonthly.IsChecked = true;
                        break;
                    case RecurrenceType.Yearly:
                        rbYearly.IsChecked = true;
                        break;
                }

                switch (_recurrenceInfo.WeekDays)
                {
                    case WeekDays.EveryDay:
                        cbWeekDays.SelectedIndex = 9;
                        cbWeekDays1.SelectedIndex = 9;
                        break;
                    case WeekDays.WeekendDays:
                        cbWeekDays.SelectedIndex = 8;
                        cbWeekDays1.SelectedIndex = 8;
                        break;
                    case WeekDays.WorkDays:
                        cbWeekDays.SelectedIndex = 7;
                        cbWeekDays1.SelectedIndex = 7;
                        break;
                    case WeekDays.Monday:
                        cbWeekDays.SelectedIndex = 0;
                        cbWeekDays1.SelectedIndex = 0;
                        break;
                    case WeekDays.Tuesday:
                        cbWeekDays.SelectedIndex = 1;
                        cbWeekDays1.SelectedIndex = 1;
                        break;
                    case WeekDays.Wednesday:
                        cbWeekDays.SelectedIndex = 2;
                        cbWeekDays1.SelectedIndex = 2;
                        break;
                    case WeekDays.Thursday:
                        cbWeekDays.SelectedIndex = 3;
                        cbWeekDays1.SelectedIndex = 3;
                        break;
                    case WeekDays.Friday:
                        cbWeekDays.SelectedIndex = 4;
                        cbWeekDays1.SelectedIndex = 4;
                        break;
                    case WeekDays.Saturday:
                        cbWeekDays.SelectedIndex = 5;
                        cbWeekDays1.SelectedIndex = 5;
                        break;
                    case WeekDays.Sunday:
                        cbWeekDays.SelectedIndex = 6;
                        cbWeekDays1.SelectedIndex = 6;
                        break;
                }

                cbMonday.IsChecked = (_recurrenceInfo.WeekDays & WeekDays.Monday) != 0;
                cbTuesday.IsChecked = (_recurrenceInfo.WeekDays & WeekDays.Tuesday) != 0;
                cbWednesday.IsChecked = (_recurrenceInfo.WeekDays & WeekDays.Wednesday) != 0;
                cbThursday.IsChecked = (_recurrenceInfo.WeekDays & WeekDays.Thursday) != 0;
                cbFriday.IsChecked = (_recurrenceInfo.WeekDays & WeekDays.Friday) != 0;
                cbSaturday.IsChecked = (_recurrenceInfo.WeekDays & WeekDays.Saturday) != 0;
                cbSunday.IsChecked = (_recurrenceInfo.WeekDays & WeekDays.Sunday) != 0;

                if (_recurrenceInfo.WeekOfMonth == WeekOfMonth.None)
                {
                    rbNoneWeekOfMonth.IsChecked = true;
                    rbNoneWeekOfMonth1.IsChecked = true;
                }
                else
                {
                    rbWeekOfMonth.IsChecked = true;
                    rbWeekOfMonth1.IsChecked = true;
                    cbWeekOfMonth.SelectedIndex = (int)_recurrenceInfo.WeekOfMonth - 1;
                    cbWeekOfMonth1.SelectedIndex = (int)_recurrenceInfo.WeekOfMonth - 1;
                }

                switch (_recurrenceInfo.Range)
                {
                    case RecurrenceRange.NoEndDate:
                        rbNoEndDate.IsChecked = true;
                        break;
                    case RecurrenceRange.OccurrenceCount:
                        rbOcurrenceCount.IsChecked = true;
                        iuOcurrenceCount.Value = _recurrenceInfo.OccurrenceCount;
                        break;
                    case RecurrenceRange.EndByDate:
                        rbEndDate.IsChecked = true;
                        dtpEndDate.Value = _recurrenceInfo.End;
                        break;
                }
            }
        }

        public TaskEditorView()
        {
            InitializeComponent();
        }

        private void Daily_Click(object sender, RoutedEventArgs e)
        {
            if (rbDaily.IsChecked == true)
            {
                RecurrenceInfo.Type = RecurrenceType.Daily;
            }
        }

        private void Weekly_Click(object sender, RoutedEventArgs e)
        {
            if (rbWeekly.IsChecked == true)
            {
                RecurrenceInfo.Type = RecurrenceType.Weekly;
            }
        }

        private void Monthly_Click(object sender, RoutedEventArgs e)
        {
            if (rbMonthly.IsChecked == true)
            {
                RecurrenceInfo.Type = RecurrenceType.Monthly;
            }
        }

        private void Yealy_Click(object sender, RoutedEventArgs e)
        {
            if (rbYearly.IsChecked == true)
            {
                RecurrenceInfo.Type = RecurrenceType.Yearly;
            }
        }

        private void rbEveryDay_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton.IsChecked == true)
            {
                RecurrenceInfo.WeekDays = WeekDays.EveryDay;
            }
        }

        private void rbWorkDay_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton.IsChecked == true)
            {
                RecurrenceInfo.WeekDays = WeekDays.WorkDays;
            }
        }

        private void cbMonday_Click(object sender, RoutedEventArgs e)
        {
            if (cbMonday.IsChecked == true)
            {
                RecurrenceInfo.WeekDays |= WeekDays.Monday;
            }
            else
            {
                RecurrenceInfo.WeekDays &= (~WeekDays.Monday);
                if ((int)RecurrenceInfo.WeekDays == 0)
                {
                    RecurrenceInfo.WeekDays = WeekDays.Monday;
                    cbMonday.IsChecked = true;
                }
            }
        }

        private void cbTuesDay_Click(object sender, RoutedEventArgs e)
        {
            if (cbTuesday.IsChecked == true)
            {
                RecurrenceInfo.WeekDays |= WeekDays.Tuesday;
            }
            else
            {
                RecurrenceInfo.WeekDays &= (~WeekDays.Tuesday);
                if ((int)RecurrenceInfo.WeekDays == 0)
                {
                    RecurrenceInfo.WeekDays = WeekDays.Monday;
                    cbMonday.IsChecked = true;
                }
            }
        }

        private void cbWednesday_Click(object sender, RoutedEventArgs e)
        {
            if (cbWednesday.IsChecked == true)
            {
                RecurrenceInfo.WeekDays |= WeekDays.Wednesday;
            }
            else
            {
                RecurrenceInfo.WeekDays &= (~WeekDays.Wednesday);
                if ((int)RecurrenceInfo.WeekDays == 0)
                {
                    RecurrenceInfo.WeekDays = WeekDays.Monday;
                    cbMonday.IsChecked = true;
                }
            }
        }

        private void cbThursday_Click(object sender, RoutedEventArgs e)
        {
            if (cbThursday.IsChecked == true)
            {
                RecurrenceInfo.WeekDays |= WeekDays.Thursday;
            }
            else
            {
                RecurrenceInfo.WeekDays &= (~WeekDays.Thursday);
                if ((int)RecurrenceInfo.WeekDays == 0)
                {
                    RecurrenceInfo.WeekDays = WeekDays.Monday;
                    cbMonday.IsChecked = true;
                }
            }
        }

        private void cbFriday_Click(object sender, RoutedEventArgs e)
        {
            if (cbFriday.IsChecked == true)
            {
                RecurrenceInfo.WeekDays |= WeekDays.Friday;
            }
            else
            {
                RecurrenceInfo.WeekDays &= (~WeekDays.Friday);
                if ((int)RecurrenceInfo.WeekDays == 0)
                {
                    RecurrenceInfo.WeekDays = WeekDays.Monday;
                    cbMonday.IsChecked = true;
                }
            }
        }

        private void cbSaturday_Click(object sender, RoutedEventArgs e)
        {
            if (cbSaturday.IsChecked == true)
            {
                RecurrenceInfo.WeekDays |= WeekDays.Saturday;
            }
            else
            {
                RecurrenceInfo.WeekDays &= (~WeekDays.Saturday);
                if ((int)RecurrenceInfo.WeekDays == 0)
                {
                    RecurrenceInfo.WeekDays = WeekDays.Monday;
                    cbMonday.IsChecked = true;
                }
            }
        }

        private void cbSunday_Click(object sender, RoutedEventArgs e)
        {
            if (cbSunday.IsChecked == true)
            {
                RecurrenceInfo.WeekDays |= WeekDays.Sunday;
            }
            else
            {
                RecurrenceInfo.WeekDays &= (~WeekDays.Sunday);
                if ((int)RecurrenceInfo.WeekDays == 0)
                {
                    RecurrenceInfo.WeekDays = WeekDays.Monday;
                    cbMonday.IsChecked = true;
                }
            }
        }

        private void rbNoneWeekOfMonth_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton.IsChecked == true)
            {
                RecurrenceInfo.WeekOfMonth = WeekOfMonth.None;
            }
        }

        private void cbWeekOfMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cbWeekOfMonth = sender as ComboBox;
            switch (cbWeekOfMonth.SelectedIndex)
            {
                case 0:
                    RecurrenceInfo.WeekOfMonth = WeekOfMonth.First;
                    break;
                case 1:
                    RecurrenceInfo.WeekOfMonth = WeekOfMonth.Second;
                    break;
                case 2:
                    RecurrenceInfo.WeekOfMonth = WeekOfMonth.Third;
                    break;
                case 3:
                    RecurrenceInfo.WeekOfMonth = WeekOfMonth.Fourth;
                    break;
                default:
                    RecurrenceInfo.WeekOfMonth = WeekOfMonth.Last;
                    break;
            }
        }

        private void cbWeekDays_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cbWeekDays = sender as ComboBox;
            switch (cbWeekDays.SelectedIndex)
            {
                case 0:
                    RecurrenceInfo.WeekDays = WeekDays.Monday;
                    break;
                case 1:
                    RecurrenceInfo.WeekDays = WeekDays.Tuesday;
                    break;
                case 2:
                    RecurrenceInfo.WeekDays = WeekDays.Wednesday;
                    break;
                case 3:
                    RecurrenceInfo.WeekDays = WeekDays.Thursday;
                    break;
                case 4:
                    RecurrenceInfo.WeekDays = WeekDays.Friday;
                    break;
                case 5:
                    RecurrenceInfo.WeekDays = WeekDays.Saturday;
                    break;
                case 6:
                    RecurrenceInfo.WeekDays = WeekDays.WorkDays;
                    break;
                case 7:
                    RecurrenceInfo.WeekDays = WeekDays.WeekendDays;
                    break;
                default:
                    RecurrenceInfo.WeekDays = WeekDays.EveryDay;
                    break;
            }
        }

        private void rbNoEndDate_Click(object sender, RoutedEventArgs e)
        {
            RadioButton rbNoEndDate = sender as RadioButton;
            if (rbNoEndDate.IsChecked == true)
            {
                RecurrenceInfo.Range = RecurrenceRange.NoEndDate;
            }
        }

        private void rbOcurrenceCount_Click(object sender, RoutedEventArgs e)
        {
            if (rbOcurrenceCount.IsChecked == true)
            {
                RecurrenceInfo.Range = RecurrenceRange.OccurrenceCount;
            }
        }

        private void StartAlert(object sender, RoutedEventArgs e)
        {
            Task task = (this.DataContext as TaskEditorViewModel).Model;
            string reminder = "";
            
            if (cbStartAlert.IsChecked == true)
            {
                reminder += "<Reminders><Reminder Subject='" 
                    + task.Subject + " 任务快要开始了！' AlertTime='"
                    + task.StartDT.ToString("yyyy/MM/dd HH:mm:ss") + "' /></Reminders>";
            }

            task.ReminderInfo = reminder;
        }

        private void TabItem_Loaded(object sender, RoutedEventArgs e)
        {
            Task task = (this.DataContext as TaskEditorViewModel).Model;

            if (task.ReminderInfo.Length > 0)
            {
                cbStartAlert.IsChecked = true;
            }
            else
            {
                cbStartAlert.IsChecked = false;
            }

            if (_recurrenceInfo == null)
            {
                return;
            }

                
        }

    }
}
